//
//  GymListViewController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/5.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//

#import "GymListViewController.h"
#import "GymListCell.h"
#import "I_Gym.h"
#import "GymManagermentRootController.h"

@interface GymListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, assign) GymListVCType type;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation GymListViewController
#pragma mark Class Method
+ (instancetype)controllerWithType:(GymListVCType)type
{
    GymListViewController *vc = [[GymListViewController alloc] init];
    vc.type = type;
    return vc;
}

#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    switch (_type) {
        case GymListVCType_Switch:
        {
            self.title = @"切换门店";
            [self setRightBarButtonWithTitle:@"管理" target:self action:@selector(manageStoreBarBtnClicked)];
        }
            break;
            
        default:
            break;
    }
    
    [self configUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestStoreList];
}

#pragma mark Config/UI
- (void)configUI
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GymListCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([GymListCell class])];
    _tableView.tableFooterView = [UIView new];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark NET
- (void)requestStoreList
{
    if (_dataSource.count == 0) {
        [self.view showLoading];
    }
    WeakSelf(self);
    [I_Gym requestGymListOnSuccess:^(NSMutableArray *gymList) {
        [weakself.view hideStatusView];
        for (M_Gym *gym in gymList) {
            if ([gym.gymId isEqualToString:RI.currentGym.gymId]) {
                gym.isSelect = YES;
                break;
            }
        }
        weakself.dataSource = gymList;
        [weakself.tableView reloadData];
    } failure:^(NSError *error) {
        if (weakself.dataSource.count == 0) {
            [weakself.view showError:error refreshBlock:^{
                [weakself requestStoreList];
            }];
        } else {
            [AV showError:error];
        }
    }];
}


#pragma mark Action
- (void)manageStoreBarBtnClicked
{
    GymManagermentRootController *manageStoreVC = [GymManagermentRootController controller];
    [self.navigationController pushViewController:manageStoreVC animated:YES];
}


#pragma mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GymListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GymListCell class])];
    [cell refreshCellWithStore:_dataSource[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    for (M_Gym *store in _dataSource) {
//        store.isSelect = NO;
//    }
    M_Gym *selectGym = _dataSource[indexPath.row];
//    selectStore.isSelect = YES;
//    [self.tableView reloadData];
    RI.currentGym = selectGym;
    [self.navigationController popViewControllerAnimated:YES];
}

@end
